package entities;

import exceptions.NonexistentEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author sha
 */
public class KomentarModel {

    public KomentarModel() {
        emf = Persistence.createEntityManagerFactory("CeritaUnyilPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public List<Komentar> getKomentar(long idcerita) {
        List<Komentar> komentar = new ArrayList<Komentar>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT a FROM Komentar AS a LEFT JOIN a.cerita c WHERE c.id=" + idcerita);
            //q.setParameter("idcerita", 1);
            komentar = q.getResultList();

        } finally {
            em.close();
        }
        return komentar;
    }

    public Komentar findKomentar(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Komentar.class, id);
        } finally {
            em.close();
        }
    }

    public void rekamKomentar(Komentar komentar) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(komentar);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void updateKomentar(Komentar komentar) {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            em.merge(komentar);
            em.getTransaction().commit();
        } finally {
            em.close();
        }
    }

    public void deleteKomentar(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Komentar komentar;
            try {
                komentar = em.getReference(Komentar.class, id);
                komentar.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
            }
            em.remove(komentar);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public Komentar findKomentar(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Komentar.class, id);
        } finally {
            em.close();
        }
    }

    public void edit(Komentar komentar) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            komentar = em.merge(komentar);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(long idkomentar) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Komentar komentar;
            try {
                komentar = em.getReference(Komentar.class, idkomentar);
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("user belum dipilih.", enfe);
            }
            em.remove(komentar);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
}